image_list = []
image_index = 0

function getContentFromIframe(iFrameName)
{
 
    var myIFrame = document.getElementById(iFrameName);
    var content = myIFrame.contentWindow.document.body.innerHTML;
	return content
}

function draw_image_base_on_list(){
	if(image_list.length == 0) return

	img_path = image_list[image_index]

	document.getElementById('image_location_id').style.backgroundImage = "url('" + img_path + "')" 
	document.getElementById('image_location_id').style.setProperty('background-size',"contain")
	document.getElementById('image_location_id').style.setProperty('background-repeat',"no-repeat")
	document.getElementById('image_location_id').style.setProperty('background-position',"center")
}

function draw_upload(){
	document.getElementById('file_upload_form').onsubmit=function() {
		document.getElementById('file_upload_form').target = 'upload_target'; //'upload_target' is the name of the iframe
	}
	document.getElementById("upload_target").onload=function(){
		v = getContentFromIframe("upload_target")

		if(document.getElementById("fileUploadID").value != ''){
			if((v.search('error :') == 0) && (getContentFromIframe("upload_target") != '')){
				alert(v)
			}else{
				alert('Thank you for uploading the image, try refreshing the page to see your addition.')
			}
		}
	}

}
function previousPic(){
	if(image_list.length == 0) return

	image_index = image_index - 1
	if(image_index == -1) image_index = image_list.length - 1
	draw_image_base_on_list()
}

function nextPic(){
	if(image_list.length == 0) return
	image_index += 1
	if(image_index == image_list.length) {
		image_index = 0
	}
	draw_image_base_on_list()



}

function get_picture_list(){
	var xhr = getAjaxObj()
	val = "/get_picture_list"

	xhr.open('GET', val,  true); 
	xhr.onreadystatechange  = function() { 
		if(AjaxCheck(xhr) == 1) {
			if(xhr.responseText == '0') {
			
			}else{
				image_list = JSON.parse(xhr.responseText)
				draw_image_base_on_list()
				draw_upload()
			}
		} 
		else if(AjaxCheck(xhr) == -1){
			//Ignore this signal
		}
		else{
			alert('Internet or server failure, check your connection..')
		}
	}; 

	xhr.send(null); 
}
